import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/components/article_card.dart';
import 'package:flutter_app/components/singer_card.dart';
import 'package:flutter_app/components/video_card.dart';
import 'package:flutter_app/model/article_model.dart';
import 'package:flutter_app/model/user_model.dart';
import 'package:flutter_app/model/video_model.dart';
import 'package:flutter_app/services/article_service.dart';
import 'package:flutter_app/services/user_service.dart';
import 'package:flutter_app/services/video_service.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class ArticlePage extends StatefulWidget {
  ArticlePage({Key key}) : super(key: key);

  @override
  ArticlePageState createState() => ArticlePageState();
}

class ArticlePageState extends State<ArticlePage>
    with AutomaticKeepAliveClientMixin {
  EasyRefreshController _controller;

  // 独立的滚动视图 防止tab切换时触发下拉刷新
  ScrollController _scrollController;
  List<ArticleItem> _articleList = ArticleList([]).list;
  int page = 1;
  int limit = 10;
  bool hasMore = true;
  bool loading = true;
  bool error = false;
  String errorMsg = '';

//  String userType = 'DQ_SINGER';
  String userType = '';

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
    _scrollController = ScrollController();
    _getSingers();
  }

  @override
  void dispose() {
    _controller.dispose();
    _scrollController.dispose();
    super.dispose();
  }

  Future _getSingers({bool push = false}) async {
    try {
      //获取数据
//      Map<String, dynamic> result =
//          await ArticleService().getArticles(page: page, limit: limit);
      Map<String, dynamic> result;
      String data = await rootBundle.loadString("assets/data/article.json");
      print(data);
      result = jsonDecode(data);
      //将数据转为实体类
      ArticleList videoListModel = ArticleList.fromJson(result['data']);
      setState(() {
        hasMore = page * limit < result['total'];
        page++;
        if (push) {
          _articleList = videoListModel.list;
        } else {
          _articleList.addAll(videoListModel.list);
        }
      });
    } catch (e) {
      setState(() {
        error = true;
        errorMsg = e.toString();
      });
    } finally {
      setState(() {
        loading = false;
      });
    }
  }

  //上拉加载
  Future _onRefresh() async {
    if (error) {
      setState(() => error = false);
    }
    page = 1;
    await _getSingers();
    _controller.resetLoadState();
  }

  //下拉刷新
  Future _onLoad() async {
    if (hasMore) {
      await _getSingers(push: false);
    }
    _controller.finishLoad(noMore: !hasMore);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return EasyRefresh(
      // 开启首次加载刷新
      firstRefresh: true,
      // 加载时的loading
      // 空视图、异常视图

      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      controller: _controller,
      header: ClassicalHeader(),
      footer: ClassicalFooter(),
      onRefresh: _onRefresh,
      onLoad: _onLoad,
      child: ListView.builder(
          controller: _scrollController,
          itemCount: _articleList.length,
          itemBuilder: (BuildContext context, int index) {
            return Column(
              children: [
                SizedBox(height: 10),
                ArticleCard(articleItem: _articleList[index]),
              ],
            );
          }),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
  ///
}
